home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HyperLib 1997 Winter - Disc 1
/
HYPERLIB-1997-Winter-CD1.ISO.7z
/
HYPERLIB-1997-Winter-CD1.ISO
/
オンラインウェア
/
BUS
/
TMCM Software and Labs.sit
/
Software for TMCM 7_95
/
Files for Lab 9
/
random walk
< prev
Wrap
Text File
|
1994-05-08
|
2KB
|
41 lines
{ This program implements a "random walk" in which the
turtle repeatedly chooses a direction at random---either
up, down, left, or right---and moves one unit in that
direction. It continues until it reaches the edge of
the 60-by-60 square that can be viewed in the program
xTurtle. }
DECLARE directionNum { Gives a name to a memory location. A value can
be stored in this location using an assignment
statement. Later, that value can be accessed
simply by refering to the name of the memory
location. A named memory location is called
a "variable". }
LOOP
directionNum := randomInt(4) { Assignment statement stores a value into
the variable directionNum. The value is
computed as the formula "randomInt(4)". The
value of this formula is either 1, 2, 3
or 4, chosen at random; each possible choice
is equally likely. }
IF directionNum = 1 THEN { IF statement decides which direction to face }
face(0)
OR IF directionNum = 2 THEN { Note that there are four possible }
face(90) { actions in this IF statement: face(0), }
OR IF directionNum = 3 THEN { face(90), face(-90) and face(180). Only }
face(-90) { one of these actions is taken, depending }
OR IF directionNum = 4 THEN { on the value of the variable, directionNum. }
face(180)
END IF
forward(1) { and then the turtle moves forward one unit }
EXIT IF (xcoord > 29) OR (xcoord < -29) OR (ycoord > 29) OR (ycoord < -29)
{ loop ends when the horizontal or vertical position of the turtle
moves out of the range -29 to 29 }
END LOOP